/* 排序
* 
* 本题:
    归并排序求逆序对数量

*/

#define DEBUG
#pragma GCC optimize("O1,O2,O3,Ofast")
#pragma GCC optimize("no-stack-protector,unroll-loops,fast-math,inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,ssse3")

#include <iostream>   
using namespace std;
#define int long long
const int N = 5e5+10;
int n, tmp[N], q[N];

int merge_sort(int l, int r)
{
    if(l==r) return 0;
    int mid = l + r >> 1;
    int res = merge_sort(l, mid) + merge_sort(mid+1, r);
    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r)
        if(q[i] <= q[j]) tmp[k++] = q[i++]; //前一个数<后一个数则先放前一个数
        else {
            res += mid - i + 1;
            tmp[k++] = q[j++];
        }

    while(i <= mid) tmp[k++] = q[i++];
    while(j <= r) tmp[k++] = q[j++];

    for(i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];

    return res;
}

signed main()
{
    #ifdef DEBUG
        freopen("./in.txt", "r", stdin);
    #else
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #endif
    
    while(cin >> n, n)
    {
        for(int i = 0; i < n; i++) cin >> q[i];

        printf("%lld\n", merge_sort(0, n-1));
    }
    
    return 0;
}